<template>
    <h1>希望小学</h1>
    姓名：<input type="text" v-model="uname"> <br>
    年龄：<input type="text" v-model="age"> <br>
    <button @click="addStudent">添加学生</button> <br>
    <button @click="addStudentPro">通过自定义事件添加学生</button> <br>
</template>

<script>
// npm i nanoid 生成随机id
import {nanoid} from 'nanoid'
export default {
    name:'School',
    data(){
        return{
            uname:'',
            age:''
        }
    },
    methods:{
        addStudentPro(){
            const user = {id:nanoid(),'uname':this.uname,'age':this.age};
            //as是事件名 user是参数
            this.$emit('as',user);
        },
        addStudent(){
            const user = {'uname':this.uname,'age':this.age};
            this.addStu(user);
        }
    },
    props:['addStu']
}
</script>

<style>

</style>